package com.bluejie.bluej.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Async;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.concurrent.FutureCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.dc.pr.PRError;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class HttpTest {
    private static Logger log = LoggerFactory.getLogger(HttpTest.class);
    private static final HttpUtil httpUtil = new HttpUtil();
    private static  final String NAME = "thread_bluej_";
    private static  AtomicInteger atomicInteger = new AtomicInteger();

    public static void main(String[] args) {
        h1();
    }

    private static void logTest(){
        log.info("--info--");
        log.error("--error--");
    }

    private static void h1(){
        ExecutorService executorService = Executors.newFixedThreadPool(2, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                //thread.setDaemon(true);
                final String threadName = NAME + atomicInteger.getAndIncrement();
                thread.setName(threadName);
                thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    public void uncaughtException(Thread t, Throwable e) {
                        log.error(threadName, e);
                    }
                });
                return thread;
            }
        });
        Async async = Async.newInstance().use(executorService);

        Request[] requests = new Request[]{
                Request.Get("http://www.google.com/"),
                Request.Get("http://www.baidu.com/"),
                Request.Get("http://www.apache.com/"),
                Request.Get("http://www.apple.com/"),
                Request.Get("https://item.m.jd.com/ware/detail.json?wareId=11089083277")

        };

        Queue<Future<Content>> futureQueue = new LinkedList<>();
        for (final Request request: requests) {
            Future<Content> future = async.execute(request, new FutureCallback<Content>() {
                @Override
                public void completed(final Content content) {
                   log.info("--completed--"+request+"---"+System.currentTimeMillis()+"---content:"+content.asString().length());
                    //System.out.println("--completed--"+request+"---"+System.currentTimeMillis());
                }

                @Override
                public void failed(final Exception e) {
                    log.info("--failed--");

                }

                @Override
                public void cancelled() {
                    log.info("--cancelled--");

                }
            });

            futureQueue.add(future);
        }
        while(!futureQueue.isEmpty()) {
            Future<Content> fut = futureQueue.remove();

            try {
                log.info(String.valueOf(fut.get().asString().length()));
            } catch (Exception ex) {
                log.error("Future.error",ex);
            }
        }
        System.out.println("----Done------");
        executorService.shutdown();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private static void h2(){
        try {
            Content content =  Request.Get("https://item.m.jd.com/product/11089083277.html")
                    .execute().returnContent();
            log.info(content.asString());
        }catch (Exception e){

        }

    }

}
